local DIY = require "packages.diy_utility.diy_utility"

local skel = fk.CreateSkill {
  name = "lb__huiluan",
  tags = { Skill.Compulsory, Skill.Switch },
  dynamic_desc = function (self, player, lang)
    local choices = {
      "<b>斩</b>：令此牌视为【杀】",
      "<b>断</b>：弃置其一张牌",
      "<b>破</b>：对其造成1点伤害",
    }
    local list = player:getTableMark("lb__huiluan")
    local mark = {}
    for i, v in ipairs(list) do
      local choice = choices[v]
      if i == DIY.getSwitchState(player, "lb__huiluan") then
        choice = "<font color='#E0DB2F'>" .. choice .. "</font>"
      end
      table.insert(mark, choice)
    end
    if #mark == 0 then
      return "lb__huiluan"
    end
    return "lb__huiluan_inner:"..table.concat(mark, "；")
  end,
}

Fk:loadTranslationTable {
  ["lb__huiluan"] = "绘乱",
  [":lb__huiluan"] = "<b>锁定技</b>，<b>转换技</b>，你使用牌指定其他角色为唯一目标后，<b>斩</b>：令此牌视为【杀】；<b>断</b>：弃置其一张牌；<b>破</b>：对其造成1点伤害。" ..
      "当你于一回合发动三次〖绘乱〗后，你摸两张牌并随机打乱〖绘乱〗顺序。",
  [":lb__huiluan_inner"] = "<b>锁定技</b>，<b>转换技</b>，你使用牌指定其他角色为唯一目标后，{1}。" ..
  "当你于一回合发动三次〖绘乱〗后，你摸两张牌并随机打乱〖绘乱〗顺序。",
  ["#lb__huiluan"] = "绘乱：请弃置 %dest 一张牌",

  ["@lb__huiluan"] = "绘乱",
  ["lb__huiluan_1"] = "斩",
  ["lb__huiluan_2"] = "断",
  ["lb__huiluan_3"] = "破",

  ["$lb__huiluan1"] = "缭乱忍法，万彩绽！",
  ["$lb__huiluan2"] = "银河忍法，千变万化。",
}

skel:addEffect(fk.TargetSpecified, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player and data.to ~= player and data:isOnlyTarget(data.to) then
      local state = DIY.getSwitchState(player, skel.name)
      local list = player:getTableMark(skel.name)
      return list[state] == 3 or
          (list[state] == 1 and data.card.name ~= "slash") or
          (list[state] == 2 and not data.to:isNude())
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local state = DIY.getSwitchState(player, skel.name)
    local list = player:getTableMark(skel.name)
    DIY.changeSwitchState(player, skel.name)
    room:notifySkillInvoked(player, skel.name, list[state] == 2 and "control" or "offensive")
    player:broadcastSkillInvoke(skel.name)
    if list[state] == 1 then
      --[[
      if data.card.trueName ~= "slash" then
        player:addCardUseHistory("slash")
      end
      --]]
      data:changeCard("slash")
    elseif list[state] == 2 then
      local id = room:askToChooseCard(player, {
        target = data.to,
        skill_name = skel.name,
        flag = "he",
        prompt = "#lb__huiluan::" .. data.to.id,
      })
      room:throwCard(id, skel.name, data.to, player)
    elseif list[state] == 3 then
      room:damage {
        from = player,
        to = data.to,
        skillName = skel.name,
        damage = 1,
      }
    end
    if player:usedSkillTimes(skel.name) == 3 and not player.dead then
      player:drawCards(2, skel.name)
      if not player.dead then
        table.shuffle(list)
        room:setPlayerMark(player, skel.name, list)
      end
    end
    if not player.dead then
      list = player:getTableMark(skel.name)
      local mark = {}
      for i, v in ipairs(list) do
        local choice = Fk:translate("lb__huiluan_" .. v)
        if i == DIY.getSwitchState(player, skel.name) then
          choice = "<font color='red'>" .. choice .. "</font>"
        end
        table.insert(mark, choice)
      end
      room:setPlayerMark(player, "@lb__huiluan", #mark == 0 and 0 or table.concat(mark, "/"))
    end
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  DIY.setSwitchState(player, skel.name, 1, 3)
  player.room:setPlayerMark(player, skel.name, { 1, 2, 3 })
  local list = player:getTableMark(skel.name)
  local mark = {}
  for i, v in ipairs(list) do
    local choice = Fk:translate("lb__huiluan_" .. v)
    if i == DIY.getSwitchState(player, skel.name) then
      choice = "<font color='red'>" .. choice .. "</font>"
    end
    table.insert(mark, choice)
  end
  player.room:setPlayerMark(player, "@lb__huiluan", #mark == 0 and 0 or table.concat(mark, "/"))
end)

skel:addLoseEffect(function(self, player, is_death)
  DIY.removeSwitchSkill(player, skel.name)
  player.room:setPlayerMark(player, skel.name, 0)
  player.room:setPlayerMark(player, "@lb__huiluan", 0)
end)

return skel
